//
//  shareView.m
//  TryShopping
//
//  Created by panquanReabam.com on 16/6/17.
//  Copyright © 2016年 RuiBen. All rights reserved.
//

#import "ShareView.h"
#import "UMSocial.h"
@interface ShareView()<UIScrollViewDelegate,UMSocialUIDelegate>
@end

@implementation ShareView
{
  UIImage *image1;
}
@synthesize mDelegate;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        UIScrollView*shareScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, ScreenWidth,245)];
        shareScroll.delegate = self;
        shareScroll.userInteractionEnabled = YES;
        shareScroll.scrollEnabled = YES;
        shareScroll.showsVerticalScrollIndicator = FALSE;
        shareScroll.showsHorizontalScrollIndicator = FALSE;
        shareScroll.backgroundColor=[UIColor whiteColor];
        [self  addSubview:shareScroll];
        NSArray*nameBtn=[[NSArray alloc]initWithObjects:@"发送給朋友",@"分享到朋友圈",nil];
        NSArray *arrayimage = @[@"faweixin",@"fenxiangpenyouquan"];
        for (int i = 0; i<2; i++) {
            UIButton *nameButcenter = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/3*i,   80*ScreenHeight/1334+15, ScreenWidth/3, 60)];
            
            [shareScroll addSubview:nameButcenter];
            UIButton *nameBut = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/3*i,   nameButcenter.bottom+20, ScreenWidth/3, 14)];
            [nameBut setTitle: [nameBtn objectAtIndex:i]  forState:UIControlStateNormal];
            nameBut.tag=201+i;
            [nameBut setTitleColor:shenhuiColor forState:0];
            nameBut.titleLabel.font=[UIFont systemFontOfSize:14];
            [nameBut addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
            [shareScroll addSubview:nameBut];
            UIButton *wuyebuttonimage = [UIButton buttonWithType:UIButtonTypeCustom];
            wuyebuttonimage.frame = CGRectMake(ScreenWidth/3*i,  10, 60, 60);
            
            wuyebuttonimage.tag = 201+i;
            wuyebuttonimage.center=nameButcenter.center;
            [wuyebuttonimage setImage:[UIImage imageNamed: [arrayimage objectAtIndex:i]] forState:UIControlStateNormal];
            [wuyebuttonimage addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
            [shareScroll addSubview:wuyebuttonimage];
        }
        UIButton*cancel = [[UIButton alloc] initWithFrame:CGRectMake(0,200,  ScreenWidth, 45)];
        [cancel setTitle:@"取消" forState:UIControlStateNormal];
        cancel.titleLabel.font=[UIFont systemFontOfSize:15];
        cancel.backgroundColor=[UIColor whiteColor];
        [cancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [cancel addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:cancel];
        UILabel*line=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, ScreenWidth-20, 0.40)];
        line.backgroundColor=qianhuiColor;
        [cancel addSubview:line];

        
    }
    return self;
}
-(void)cancel
{
    [mDelegate cancel:@""];
}

-(void)share:(UIButton*)sender{
//    image1 = [UIImage imageNamed:@"桌面启动图标（100）.png"];
   
    
    
    if ([[self.dataDict objectForKey:@"imageUrl"]length]>0) {
        
        NSURL *url = [NSURL URLWithString:[self.dataDict objectForKey:@"imageUrl"]];
        NSData *data = [NSData dataWithContentsOfURL:url];
        image1 = [[UIImage alloc] initWithData:data];
        if (data==NULL) {
            NSString *str1=[NSString stringWithFormat:@"%d",TARGET_VERSION];
            NSString *plistPath1 = [[NSBundle mainBundle] pathForResource:str1 ofType:@"plist"];
            NSDictionary *dictionary1 = [[NSDictionary alloc] initWithContentsOfFile:plistPath1];
            NSString *logoStr = [dictionary1 objectForKey:@"logoStr"];
            image1 = [UIImage imageNamed:logoStr];
        
        }
       }else{

        NSString *str1=[NSString stringWithFormat:@"%d",TARGET_VERSION];
        NSString *plistPath1 = [[NSBundle mainBundle] pathForResource:str1 ofType:@"plist"];
        NSDictionary *dictionary1 = [[NSDictionary alloc] initWithContentsOfFile:plistPath1];
        NSString *logoStr = [dictionary1 objectForKey:@"logoStr"];
        image1 = [UIImage imageNamed:logoStr];
    }
    
            if (sender.tag==201) {
       
        [UMSocialData defaultData].extConfig.wechatSessionData.title =  [self.dataDict objectForKey:@"title"];
        UMSocialUrlResource *urlResource = [[UMSocialUrlResource alloc] initWithSnsResourceType:UMSocialUrlResourceTypeDefault url:[self.dataDict objectForKey:@"shareDescription"]];
        [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession].snsClickHandler(self,[UMSocialControllerService defaultControllerService],YES);
        [UMSocialData defaultData].extConfig.wechatSessionData.url = [self.dataDict objectForKey:@"shareUrl"];
        [[UMSocialDataService defaultDataService]postSNSWithTypes:@[UMShareToWechatSession] content:[self.dataDict objectForKey:@"shareDescription"] image:image1 location:nil urlResource:urlResource presentedController:self completion:^(UMSocialResponseEntity *response){
            
            if (response.responseCode == UMSResponseCodeSuccess) {
//                NSLog(@"分享成功！");
//               [Global showTextHudSub:@"分享成功！" view:self];
//               [self ShareRecords];
              }
            
        }];
    }
    if (sender.tag==202) {
        
        UMSocialUrlResource *urlResource = [[UMSocialUrlResource alloc] initWithSnsResourceType:UMSocialUrlResourceTypeDefault url:[self.dataDict objectForKey:@"shareUrl"]];
        [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatTimeline].snsClickHandler(self,[UMSocialControllerService defaultControllerService],YES);
        [UMSocialData defaultData].extConfig.wechatTimelineData.url = [self.dataDict objectForKey:@"shareUrl"];
        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatTimeline] content:[NSString stringWithFormat:@"%@",    [self.dataDict objectForKey:@"title"]] image:image1 location:nil urlResource:urlResource presentedController:self completion:^(UMSocialResponseEntity *response){
            if (response.responseCode == UMSResponseCodeSuccess) {
                NSLog(@"分享成功！");
                  [Global showTextHudSub:@"分享成功！" view:self];
//                  [self ShareRecords];
                }
        }];
    }
    
}
-(void)ShareRecords{
    
          [self.mDelegate successfullyReturns:@""];
   
}

@end
